package com.feicheng.iou.controller.image;

import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.oss.OssService;
import com.feicheng.iou.result.ResultInfoV110;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author tianchunlei
 * @Des //文件上传接口
 * @Date 2021/8/17
 **/
@RestController
@Slf4j
@RequestMapping("/iou/oss/file")
public class FileUploadController {

    @Autowired
    private OssService ossService;

    /**
     * @return ResultInfoV110
     * @Author tianchunlei
     * @Description //文件上传接口方法
     * @Date 2021/8/17 22:39
     * @Param file接收前端上传的文件  module业务类型
     */
    @RequestMapping("/upload")
    public ResultInfoV110 upload(MultipartFile file, String module) {

        log.info("FileUploadController的upload接口进入，参数file= " + file + ",module= " + module);

        //定义接收文件上传后得到的文件存储的url
        String url;

        //参数校验
        if (file == null || module == null) {
            throw new SystemExceptionV110(
                    InformationBean.setInformationBean(
                            StatusCodeConstant.F002, "文件上传时参数")
            );
        }
        //调用文件上传的业务类
        try {
            url = ossService.upload(file.getInputStream(), module, file.getOriginalFilename());
        } catch (IOException e) {
            throw new SystemExceptionV110(
                    InformationBean.setInformationBean(
                            StatusCodeConstant.F001, "文件上传")
            );
        }

        Map<String,String> map = new HashMap<>();
        map.put("URL",url);

        return ResultInfoV110.success(map);
    }

}
